package com.example.springbootdemo.database;

import java.sql.*;

public class QueryHelper {

    static ResultSet resultSet;
    static Connection connection;
    static Statement statement;

    public static ResultSet execuQuery(String sql) {
        if(connection==null){
            connection = DataBase.getConnection();
        }
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return resultSet;
    }


    public static int execuUpdate(String sql) {
        if(connection==null){
            connection = DataBase.getConnection();
        }
        int result = 0;
        try {
            statement = connection.createStatement();
            result = statement.executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public static void closeResource() {
        try {
            if (connection != null) {
                connection.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
